Source Code Viewer - Example 5 - FindUserRatingListByRatingAction

package name.matthewgreet.example5.action;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpSession;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

import name.matthewgreet.example5.common.AbstractFormDrivenActionSupport;
import name.matthewgreet.example5.common.ListCache;
import name.matthewgreet.example5.common.ListFinder;
import name.matthewgreet.example5.dto.GameDTO;
import name.matthewgreet.example5.dto.UserRatingDTO;
import name.matthewgreet.example5.form.UserRatingRatingForm;
import name.matthewgreet.example5.model.Model;
import name.matthewgreet.example5.model.UserRatingPK;
import name.matthewgreet.example5.model.UserRatingTable;
import name.matthewgreet.example5.util.GameCompositeCache;

/**
 * Finds user ratings for a game by its rating band.  Game ratings can have one decimal place and ratings are in the 
 * same band if they have the same integer part, from 0 to 10.    
 */
@InterceptorRef("FormDrivenStack")
@Results({
	@Result(name=ActionSupport.INPUT, type="redirectAction", params = {"namespace", "/", "actionName", "ViewUserRatingList"}),
	@Result(name=ActionSupport.SUCCESS, type="redirectAction", params = {"namespace", "/", "actionName", "ViewUserRatingList"})
})
public class FindUserRatingListByRatingAction extends AbstractFormDrivenActionSupport<UserRatingRatingForm> {
	public static class UserRatingByRatingListFinder implements ListFinder<GameDTO,UserRatingDTO> {
		private static final long serialVersionUID = 9200935020719468634L;
		
		private Integer rating;
		
		public UserRatingByRatingListFinder(Integer rating) {
			this.rating = rating;
		}

		@Override
		public List<UserRatingDTO> getList(GameDTO selectedMaster) throws Exception {
			UserRatingTable userRatingTable;
			BigDecimal rangeStart, rangeEnd;
			
			
			userRatingTable = Model.getInstance().getUserRatingTable();
			if (rating != null) {
				rangeStart = new BigDecimal(rating);
				rangeEnd = rangeStart.add(new BigDecimal("0.9"));
				return userRatingTable.findByGameIdAndRatingOrdered(selectedMaster.getId(), rangeStart, rangeEnd);
			} else {
				return userRatingTable.findByGameIdOrdered(selectedMaster.getId());
			}
		}
		
		public Integer getRating() {
			return rating;
		}

	}
	
	
	private static final long serialVersionUID = -7979376337555804889L;
	
    @Action("/FindUserRatingListByRating")
    @Override
    public String execute() throws Exception {
    	UserRatingRatingForm form;
    	ListCache<GameDTO,UserRatingPK,UserRatingDTO> listCache;
    	HttpSession session;
    	
    	form = getModel();
    	
    	session = getRequest().getSession();
    	listCache = GameCompositeCache.getInstance(session).getSlaveListCache2();
    	listCache.setBaseRecordListFinder(new UserRatingByRatingListFinder(form.getParsedRating()));
		
		return SUCCESS;
    }

	@Override
	protected UserRatingRatingForm makeForm() {
		return new UserRatingRatingForm();
	}

}